package com.game.bullet;

import android.graphics.Point;

import com.game.character.Character;
import com.game.character.TypeCharacter.TypeObject;
import com.game.collision.CollisionCheck;
import com.game.framwork.gamesupport.GameSprite;
import com.game.framwork.itf.Graphics;
import com.game_ltdd.Resrouce;

public class Bullet extends Character {

	private BulletExploreEffect bulletExploreEffect;
	public Character user;
	public float timeLife;
	public float maxTimeLife;
	public float timeExplore;
	public float maxTimeExplore;
	public int boomStr;
	public int speedBonus = 3;

	public Bullet() {
		init();
	}
	
	private void init() {
		type = TypeObject.Bullet;
		this.objectImage = Resrouce.bulletImage;
		GameSprite[] pAnim;
		pAnim = new GameSprite[2];
		pAnim[0] = new GameSprite(objectImage);
		pAnim[0].createAnimation("Boom", 0, 3, 0, 1.0f / 4.0f);
		pAnim[1] = new GameSprite(objectImage);
		pAnim[1].createAnimation("Explore", 4, 7, 4, 1.0f / 4.0f);
		loadSprite(objectImage, 8, 8, 46, 49, pAnim);
		createBoundBox(0, 0, 30, 30);
		
		changeAnimation("Boom");
		bulletExploreEffect = new BulletExploreEffect();
	}
	
	private float getSpeedBullet(Character user) {
		switch (user.currState) {
		case MOVE_LEFT:
			currentSpeed = user.currentSpeed - speedBonus;
			break;
		case MOVE_RIGHT:
			currentSpeed = user.currentSpeed + speedBonus;
			break;
		case MOVE_TOP:
			currentSpeed = user.currentSpeed - speedBonus;
			break;
		case MOVE_BOTTOM:
			currentSpeed = user.currentSpeed + speedBonus;
			break;
		}
		return boomStr;
	}
	
	public void initInfo(Character user) {
		//time
		timeLife = 0.0f;
		maxTimeLife = 1.0f;
		timeExplore = 0.0f;
		maxTimeExplore = 2.0f;
		// info
		this.user = user;
		//for render
		this.isDeath = false;
		isExplore = false;
		getSpeedBullet(this.user);
		currState = this.user.currState;
		this.minimumDamge = this.user.minimumDamge;
		this.position = this.user.position;
	}

	public void explore() {
		isExplore = true;
		timeLife = 0.0f;
		isDeath = true;
	}
	
	private void checkExplor() {
		CollisionCheck.collisionOnMap(this, tiledMap);
		if (isExplore) {
			return;
		}
		if (getPossition().x - getBoundBox().width() / 2 < 0) {
			isExplore = true;
			setPossition(new Point(getBoundBox().width() / 2, getPossition().y));
			return;
		}
		if (getPossition().y - getBoundBox().height() / 2 < 0) {
			isExplore = true;
			setPossition(new Point(position.x, getBoundBox().height() / 2));
			return;
		}
		if (getPossition().x + getBoundBox().width() / 2 > tiledMap.mapWidth * tiledMap.cellSize) {
			isExplore = true;
			setPossition(new Point(tiledMap.mapWidth * tiledMap.cellSize - getBoundBox().width() / 2, getPossition().y));
			return;
		}
		if (getPossition().y + getBoundBox().height() / 2 > tiledMap.mapHeight * tiledMap.cellSize) {
			isExplore = true;
			setPossition(new Point(getPossition().x, tiledMap.mapHeight * tiledMap.cellSize - getBoundBox().height() / 2));
			return;
		}
	}
	
	@Override
	public void updateInternal(float deltaTime) {
		if (!isDeath) {
			move(currentSpeed);
			checkExplor();
			if (!isExplore) {	
				timeLife += deltaTime;
				if (timeLife >= maxTimeLife) {
					explore();
				}
			} else {
				explore();
			}
			bulletExploreEffect.getCurrAnimation().resetAnimation();
			super.updateInternal(deltaTime);
			return;
		}		
		bulletExploreEffect.setPossition(position);
		bulletExploreEffect.attachTiledMap(tiledMap);
		bulletExploreEffect.updateInternal(deltaTime);
	}

	@Override
	public void paintInternal(Graphics g) {
        if(!isDeath) {	
        	super.paintInternal(g);
        	return;
        }  
        bulletExploreEffect.paintInternal(g);
    }
}
